const {ccclass, property} = cc._decorator;
import {ScrollViewCom, ScrollItemData} from "../base/scrollViewComTS";

@ccclass
export default class svTestTs extends cc.Component {

    @property(cc.Node)
    scrollView: cc.Node = null;

    private topNum:number = 0;
    private bottomNum:number = 0;
    private testSvCom:ScrollViewCom;
    private _selItem:any;
    private _selAddItem:any;

    onLoad () {
        this.testSvCom = this.scrollView.getComponent("scrollViewComTS");
        this.testSvCom.init({cb_host:this,item_setter:this.item_setter});

        var datas = [];
        for(var i = 0; i < 1000; i++)
        {
            let data:ScrollItemData = 
            {
                index: null,
                key:"item2",
                itemData: 
                {
                    labelNum: i,
                    isSel : false,
                },
            }
            datas.push(data);
        }
        this.testSvCom.set_data(datas);
    }

    item_setter(item:cc.Node, data:any) : [number, number]
    {
        switch(data.key)
        {
            case "item1":
                item!.getComponent("desItemTs")!.itemDataAssignment(data);
            break;
            default:
                item!.getComponent("ItemTs")!.itemDataAssignment(data);
            break;
        }

        return [item.width, item.height];
    }

    addBtnClick(event, type){
        if (type == 0){//添加到顶部
            console.log("顶部点了次数"+ this.topNum);
            this.topNum ++;

            let data:ScrollItemData = 
            {
                index: null,
                key: "item3",
                itemData:
                {
                    labelNum : "顶部添加"+this.topNum,
                    isSel : false,
                },
            }
            this.testSvCom.insert_data(0, data);
        }
        if (type == 1){//添加到底部
            console.log("底部点了次数"+ this.bottomNum);
            this.bottomNum ++;

            var data:ScrollItemData = 
            {
                index: null,
                key: "item",
                itemData:
                {
                    labelNum: "底部添加"+this.bottomNum,
                    isSel : false,
                }
            }
            this.testSvCom.append_data(data);
            this.testSvCom.scroll_to_end();
        }
    }

    onClickItem(index:number)
    {
        if(this._selItem)
        {
            if(this._selItem.data.index == index)
                return;

            this._selItem.data.itemData.isSel = false;
        }

        this._selItem = this.testSvCom.get_Item(index);
        this._selItem.data.itemData.isSel = true;
        this.testSvCom.Refresh_viewPort();

        //动态增删item
        if(this._selAddItem)
            this.testSvCom.remove_data(this._selAddItem.data.index);

        var data:ScrollItemData = 
        {
            key:"item1",
            itemData:
            {
                labelNum: "底部添加"+this._selItem.data.itemData.labelNum,
            }
        }
        var addItemIndex = this._selItem.data.index + 1
        this.testSvCom.insert_data(addItemIndex, data);
        this._selAddItem = this.testSvCom.get_Item(addItemIndex);
    }
}
